﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using trangbaocao.Models;
using System.Collections;
using System.Data.Entity;

namespace trangbaocao.Controllers
{
    [Authorize]
    public class GeneratorController : Controller
    {
        //
        // GET: /Generator/
        GeneratorModel GeneratorModel = new GeneratorModel();
        StationsModel StationsModel = new StationsModel();
        Main_StationsModel Main_StationsModel = new Main_StationsModel();
        public ActionResult Index()
        {
            GeneratorAssociateModel GeneratorAssociateModel = new GeneratorAssociateModel();
            List<GeneratorAssociateModel> ge = GeneratorAssociateModel.getGeneratorIndex();
            return View(ge);
        }
        public ActionResult EditGenerator(int id)
        {
            GeneratorModel GeneratorModel_ = GeneratorModel.getGenerator().Find(delegate(GeneratorModel s) { return s.id == id; });

            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            GeneratorModel_.trungtam_id = new SelectList(main_StationsModel, "id", "name");

            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(GeneratorModel_.main_stations_id);
            GeneratorModel_.tram_id = new SelectList(stationModel, "id", "name");
            return View(GeneratorModel_);
        }
        [HttpPost]
        public ActionResult EditGenerator(GeneratorModel Generator)
        {
            if (ModelState.IsValid)
            {
                Generator.updateGenerator(Generator);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("Index");
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            Generator.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Generator.main_stations_id);
            Generator.tram_id = new SelectList(stationModel, "id", "name");
            return View(Generator);
        }
    }
}
